import pandas as pd
import numpy as np

# plotting
import seaborn as sns
import matplotlib.pyplot as plt
#%matplotlib inline

# setting params
params = {'legend.fontsize': 'x-large',
          'figure.figsize': (30, 10),
          'axes.labelsize': 'x-large',
          'axes.titlesize':'x-large',
          'xtick.labelsize':'x-large',
          'ytick.labelsize':'x-large'}

sns.set_style('whitegrid')
sns.set_context('talk')

plt.rcParams.update(params)
pd.options.display.max_colwidth = 300

# pandas display data frames as tables
from IPython.display import display, HTML
train = pd.read_csv("res/day.csv")
heads = train.head()


categorical_features = ['season', 'mnth', 'weathersit', 'weekday']
for col in categorical_features:
    # print('\n%s属性的不同取值和出现的次数'%col)
    # print(train[col].value_counts())
    train[col] = train[col].astype('object')


# 2.数值特征的分布,对数值型特征，直方图
# numerical_features = ['temp', 'atemp', 'hum', 'windspeed']
# train[numerical_features].hist()
# sns.distplot()


# sns.violinplot(data=train[['yr', 'cnt']], x="yr", y="cnt")

# sns.violinplot(data=train[['season', 'cnt']], x="season", y="cnt")

import datetime

# barplot利用矩阵条的高度反映数值变量的集中趋势，以及使用errorbar功能（差棒图）来估计变量之间的差值统计。
# 谨记barplot展示的是某种变量分布的平均值
# fig, ax = plt.subplots()
# sns.barplot(data=train[['season','cnt']],x="season", y="cnt")
# ax.set(title="Seasonly distribution of counts")

# 月份与汽车数量的关系,5-10月比较多
# fig,ax = plt.subplots()
# sns.barplot(data=train[['mnth','cnt']], x="mnth",y="cnt")
# ax.set(title="Monthly distribution of counts")

# 天气和骑车数量的关系，天气好的时候骑行量比较稳定，不好的时候骑行量少而且不稳定
# fig, ax = plt.subplots()
# sns.barplot(data=train[['weathersit','cnt']],x="weathersit",y="cnt")
# ax.set(title="weathersit distribution of counts")

# 工作日和节假日的关系，工作日与否骑行量差别不大，节假日与否，差距比较大，并且节假日的时候变化幅度大
# fig,(ax1,ax2) = plt.subplots(ncols=2)
# sns.barplot(data=train,x='holiday',y='cnt',ax=ax1)
# sns.barplot(data=train,x='workingday',y='cnt',ax=ax2)

# 数值型的特征和y之间的关系,结果显示骑行量和温度有一点相关性
# corrMatt = train[["temp","atemp","hum","windspeed","cnt"]].corr()
# mask = np.array(corrMatt)
# mask[np.tril_indices_from(mask)] = False
# sns.heatmap(corrMatt,mask=mask,vmax=.8,square=True,annot=True)

# print("train : " + str(train.shape))
# print(train.info())
# print(train.describe())
# 特征工程

print('对类别型变量进行独热编码')
X_train_cat = train[categorical_features]
X_train_cat = pd.get_dummies(X_train_cat)
heads=X_train_cat.head()
print(heads)

from sklearn.preprocessing import MinMaxScaler
print('统一数值尺度')
mn_X = MinMaxScaler()
numerical_features = ['temp','atemp','hum','windspeed']
temp = mn_X.fit_transform(train[numerical_features])
X_train_num = pd.DataFrame(data=temp,columns=numerical_features,index=train.index)

print('将数值型变量和编码后的类别型变量结合起来,再加上编号和年，还有骑行数量')
X_train = pd.concat([X_train_cat,X_train_num,train['holiday'],train['workingday']],axis=1,ignore_index=False)
print(X_train.head())

FE_train = pd.concat([train['instant'],X_train,train['yr'],train['cnt']],axis=1)
FE_train.to_csv('res/FE_day.csv',index=False)
print(FE_train.head())
print(FE_train.info())

# train.plot()
plt.show()
print('over')
# while(True):
#     i = input()
#     break